package com.foxzzz.util;

import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyListenner implements Listener {
    public volatile static JavaPlugin plugin = null;
    public static List<Attribute> attris = Collections.synchronizedList(new ArrayList<>());
    public static List<ItemFlag> flags = Collections.synchronizedList(new ArrayList<>());

    static {
        attris.add(Attribute.GENERIC_ATTACK_DAMAGE);
        attris.add(Attribute.GENERIC_ATTACK_SPEED);
        attris.add(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
        attris.add(Attribute.GENERIC_MOVEMENT_SPEED);
        attris.add(Attribute.GENERIC_LUCK);
        attris.add(Attribute.GENERIC_ARMOR_TOUGHNESS);
        attris.add(Attribute.GENERIC_ARMOR);
        attris.add(Attribute.GENERIC_MAX_HEALTH);
        flags.add(ItemFlag.HIDE_UNBREAKABLE);
        flags.add(ItemFlag.HIDE_ENCHANTS);
        flags.add(ItemFlag.HIDE_ATTRIBUTES);
    }

    public MyListenner(JavaPlugin plugin) {
        if (plugin == null)
            this.plugin = plugin;
    }

    public static Player getPlayer(String player) {
        return plugin.getServer().getPlayer(player);
    }

    public static void sendMessage(String player, String message) {
        try {
            Player player1 = getPlayer(player);
            player1.sendMessage(message);
        } catch (Exception e) {

        }
    }

    public static Integer getEmptyNum(Player player) {
        ItemStack[] stacks = player.getInventory().getContents();
        Integer num = 0;
        for (int i = 0; i < stacks.length; i++) {
            if (stacks[i] == null)
                num++;
        }
        return num;
    }

    public static boolean addItem(Player player, String mojang, int num) {
        ItemStack itemStack = ItemSerializer.loadItemStackJson(mojang);
        return addItem(player, itemStack, num);
    }

    public static boolean addItem(Player player, ItemStack itemStack, int num) {
        if (itemStack == null)
            return false;
        int empty = getEmptyNum(player);
        int size = itemStack.getMaxStackSize();
        if (empty * size < num)
            return false;
        itemStack.setAmount(num);
        player.getInventory().addItem(itemStack);
        player.saveData();
        return true;
    }

    public static String getMainHandMojangSon(Player player) {
        ItemStack itemStack = player.getInventory().getItemInMainHand();
        ItemStack itemStack1 = new ItemStack(itemStack);
        itemStack1.setAmount(1);
        return itemStack == null ? null : ItemSerializer.getItemStackJson(itemStack1);
    }

    public static int getItemCount(Player player, String mojangson) {
        Player pl = player;
        ItemStack template = ItemSerializer.loadItemStackJson(mojangson);
        int count = 0;
        ItemStack[] itemStacks = pl.getInventory().getContents();
        for (int i = 0; i < itemStacks.length; i++) {
            if (itemStacks[i] != null && template.isSimilar(itemStacks[i]))
                count += itemStacks[i].getAmount();
        }
        return count;
    }

    public static boolean clearItemCount(Player player, String mojangson, int count) {
        ItemStack template = ItemSerializer.loadItemStackJson(mojangson);
        Player pl = player;
        ItemStack[] itemStacks = pl.getInventory().getContents();

        for (int i = 0; i < itemStacks.length; i++) {
            if (count == 0)
                break;
            if (itemStacks[i] != null && template.isSimilar(itemStacks[i]))
                if (count >= itemStacks[i].getAmount()) {
                    count -= itemStacks[i].getAmount();
                    pl.getInventory().clear(i);
                } else {
                    int num = itemStacks[i].getAmount();
                    itemStacks[i].setAmount(num - count);
                    pl.getInventory().setItem(i, itemStacks[i]);
                    count = 0;
                }
        }
        pl.saveData();
        return true;
    }

    public static void cpHand(Player player, int num) {
        Player pl = player;
        ItemStack itemStack = pl.getInventory().getItem(8);
        if (itemStack != null)
            addItem(player, new ItemStack(itemStack), num);
    }

    public static void openCpInv(Player player, String targetPlay) {
        if (getPlayer(targetPlay) == null)
            return;
        Player source = player;
        Player target = getPlayer(targetPlay);
        ItemStack[] itemStacks = target.getInventory().getStorageContents();
        Inventory inv = Bukkit.createInventory(null, 54, "Inv");
        for (int i = 0; i < itemStacks.length; i++) {
            if (itemStacks[i] != null)
                inv.addItem(new ItemStack(itemStacks[i]));
        }
        source.openInventory(inv);
    }

    public static void giveItem(Player player, String mojangson) {
        player.getInventory().addItem(ItemSerializer.loadItemStackJson(mojangson));
    }

    public static void getHandMojangson(Player player) {
        Player pl = player;
        String mojangson = ItemSerializer.getItemStackJson(pl.getInventory().getItem(8));
        pl.sendMessage(mojangson);
    }

    public static void levelUp(Player player, int level) {
        Player pl = player;
        pl.giveExpLevels(level);
    }

    public static void setAttribute(Player player, Integer chose, double val) {
        Player pl = player;
        if (chose == 0) {
            for (int i = 0; i < attris.size(); i++) {
                pl.getAttribute(attris.get(0)).setBaseValue(1.0);
                pl.getAttribute(attris.get(1)).setBaseValue(4.0);
                pl.getAttribute(attris.get(2)).setBaseValue(0.0);
                pl.getAttribute(attris.get(3)).setBaseValue(0.10000000149011612);
                pl.getAttribute(attris.get(4)).setBaseValue(0.0);
                pl.getAttribute(attris.get(5)).setBaseValue(0.0);
                pl.getAttribute(attris.get(6)).setBaseValue(0.0);
                pl.getAttribute(attris.get(7)).setBaseValue(20.0);
            }
        } else
            pl.getAttribute(attris.get(chose - 1)).setBaseValue(val);
    }

    public static void setEnchant(Player player, Integer enId, int level) {
        Player pl = player;
        ItemStack itemStack = pl.getInventory().getItem(8);
        if (itemStack != null)
            if (level <= 0)
                itemStack.removeEnchantment(new EnchantmentWrapper(enId));
            else
                itemStack.addUnsafeEnchantment(new EnchantmentWrapper(enId), level);
    }

    public static void hide(Player player, Integer num) {
        ItemStack itemStack = player.getInventory().getItem(8);
        if (itemStack == null)
            return;
        num = num < 0 ? 0 : num;
        ItemFlag flag = flags.get(num);
        ItemMeta itemMeta = itemStack.getItemMeta();
        boolean b = itemMeta.hasItemFlag(flag);
        if (b) {
            itemMeta.removeItemFlags(flag);
        } else {
            itemMeta.addItemFlags(flag);
        }
        itemStack.setItemMeta(itemMeta);
    }

    public static void unbreak(Player player) {
        ItemStack itemStack = player.getInventory().getItem(8);
        if (itemStack == null)
            return;
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setUnbreakable(!itemMeta.isUnbreakable());
        itemStack.setItemMeta(itemMeta);
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent evt) {
        Player p = evt.getPlayer();
    }


}
